package com.binvshe.binvshe.mine.follow.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.ParamKey;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.common.image.IML;
import com.binvshe.binvshe.mine.follow.bean.ClubBean;
import com.binvshe.binvshe.organization.ClubCenterActivity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.UrlParams;
import com.pachong.android.frameworkbase.dialog.AlertDialogFragment;
import com.pachong.android.frameworkbase.utils.EasyToast;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 */
public class FollowClubFragment extends SwipeListFragment {

    private int mPage = 0;

    public static FollowClubFragment newInstance() {
        FollowClubFragment fragment = new FollowClubFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        startLoading();
        return view;
    }

    public FollowClubFragment() {
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new FollowClubListAdapter(getActivity());
    }

    @Override
    public void onResume() {
        super.onResume();
        mPage = 0;
        startLoading();
    }

    @Override
    public void onStartLoading() {
        mPage = 0;
        getData().clear();
        getFollowData();
    }

    private ListDataRequestListener<ClubBean> mResponceListener = new ListDataRequestListener<>(this, ClubBean.class, true);

    void getFollowData() {
        if (mPage == 0 || mResponceListener.getResponseDataParser().hasMore()) {
            mPage++;
            MyHttpRequest request = new MyHttpRequest(getActivity());
            UrlParams urlParams = new UrlParams();
            urlParams.put(ParamKey.PAGE, mPage + "");
            urlParams.put(ParamKey.SIZE, 8 + "");
            request.get(UrlCenter.CLUB_FOLLOW, urlParams, mResponceListener);
        }
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        getData().clear();
        mPage = 0;
        startLoading();
    }

    public class FollowClubListAdapter extends RecyclerViewBaseAdapter<ClubBean> {


        public FollowClubListAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
            return new VH(setAdapterViewItem(parent, R.layout.item_follow_club));
        }

        @Override
        public void onBindHolder(RecyclerView.ViewHolder holder, final int position) {
            final VH viewHolder = (VH) holder;
            final ClubBean followClub = getData().get(position);
            if (!TextUtils.isEmpty(followClub.getPortrait()))
                IML.load(getContext(), viewHolder.mCirHead, followClub.getPortrait());
            if (!TextUtils.isEmpty(followClub.getName()))
                viewHolder.mTvName.setText(followClub.getName());
            if (!TextUtils.isEmpty(followClub.getCity()))
                viewHolder.mTvCity.setText(followClub.getCity());

            viewHolder.mCirHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ClubCenterActivity.start(getContext(), followClub.getId());
                }
            });

            viewHolder.mBtnFollow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDialog();
                }

                private void showDialog() {
                    final AlertDialogFragment alertDialogFragment = new AlertDialogFragment();
                    alertDialogFragment.setMessage("确定取消关注" + followClub.getName() + "社团吗?")
                            .setTitle("提示")
                            .setNegativeButton("取消", true, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    alertDialogFragment.dismiss();
                                }
                            })
                            .setPositiveButton("确定", false, new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    unFollowClubRequest();
                                    alertDialogFragment.dismiss();
                                }

                                private void unFollowClubRequest() {
                                    MyHttpRequest request = new MyHttpRequest(getActivity());
                                    String url = String.format(UrlCenter.DEL_CLUB, new Object[]{followClub.getId()});
                                    request.delete(url, null, new DataRequestListener<String>(String.class) {
                                        @Override
                                        public void onStart(Context context, String url) {
                                            enableProgress(false);
                                            super.onStart(context, url);
                                        }

                                        @Override
                                        public void onSuccess(String str) {
                                            super.onSuccess(str);
                                            EasyToast.showToast(getContext(), "取消关注社团成功!");
                                            onStartRefreshing();
                                        }

                                        @Override
                                        public void onFailure(Throwable t, int errorNo, String strMsg) {
                                            super.onFailure(t, errorNo, strMsg);
                                        }
                                    });
                                }
                            })
                            .show(getFragmentManager());
                }
            });
        }

        public class VH extends RecyclerView.ViewHolder {

            @Bind(R.id.mCirHead)
            CircleImageView mCirHead;
            @Bind(R.id.mTvName)
            TextView mTvName;
            @Bind(R.id.mBtnFollow)
            Button mBtnFollow;
            @Bind(R.id.mTvCity)
            TextView mTvCity;

            public VH(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
